//引入模块
import Api from './api.js'
import Cookie from './cookie.js'
import Md5 from './md5.js'


new Vue({
    el:"#app",
    data(){
        return{
            email:'',
            einfo:'',

            pwd:'',
        }
    },
    mounted(){

    },
    methods:{
        e1(){
            let regExp=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if(regExp.test(this.email)){
                this.flag1=1
            }else {
                this.$message({
                    showClose: true,
                    message: '您的邮箱为空或输入不规范！',
                    type: 'error'
                });
            }
        },
        async goEmail(){
            if(this.flag1==1){
                let zh = sessionStorage.getItem("zh")
                const loading = this.$loading({
                    lock: true,
                    text: '正在发送验证码...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                let yanzheng = await Api.email(zh,this.email)
                yanzheng = JSON.parse(yanzheng)
                sessionStorage.setItem("email",this.email)
                Cookie.setCookie("yz",yanzheng,'s300')
                setTimeout(() => {
                    loading.close();
                    window.location.href='./forgot2.html';
                }, 1000);
            }else {
                this.$message({
                    showClose: true,
                    message: '您的邮箱为空或输入不规范！',
                    type: 'error'
                });
            }
        },
        //重新发送邮箱验证码
        async res(){
            let zh = sessionStorage.getItem("zh")

            const loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            let yanzheng = await Api.email(zh,this.email)
            if(yanzheng!=null){
                this.$message({
                    showClose: true,
                    message: '发送成功！',
                    type: 'success'
                });
            }
            yanzheng = JSON.parse(yanzheng)
            Cookie.setCookie("yz",yanzheng,'s300')
            setTimeout(() => {
                loading.close();
                window.location.href='./forgot2.html';
            }, 1000);
        },
        //邮箱验证
        async success(){
            let email = sessionStorage.getItem("email")
            let msg = Cookie.getCookie("yz")

            let yz = Md5.md5(this.einfo)

            if(msg!=null){
                msg = JSON.parse(msg)
            }

            if (msg.data == yz){

                let regExp = /^[A-Za-z0-9]{6,14}$/
                if(!regExp.test(this.pwd)) {
                    this.$message({
                        showClose: true,
                        message: '您的密码为空或输入不规范！',
                        type: 'error'
                    });
                }else {
                    let falg = await Api.updatePwd(email,this.pwd)

                    if(falg=='0'){
                        this.$alert('修改成功！返回登录界面', '温馨提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.$message({
                                    type: 'info',
                                    message: `action: ${ action }`
                                });

                                window.location.href='./login.html';
                            }
                        });
                    }else {
                        this.$message({
                            showClose: true,
                            message: '服务器忙，请稍后重试！',
                            type: 'error'
                        });
                    }
                }

            }else {
                this.$message({
                    showClose: true,
                    message: '验证码错误或已失效！',
                    type: 'error'
                });
            }
        },
    }
})